import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import createVitePlugins from './vite/plugins'
// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd())
  const {VITE_API_HOST} = env
  return {

    plugins: createVitePlugins(env, command === 'build'),
    assetsInclude: ['**/*.xlsx'],
    resolve: {
      // https://cn.vitejs.dev/config/#resolve-alias
      alias: {
        // 设置路径
        '~': path.resolve(__dirname, './'),
        // 设置别名
        '@': path.resolve(__dirname, './src'),
      },
      // https://cn.vitejs.dev/config/#resolve-extensions
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    externals: [{
      './cptable': 'var cptable'
    }], 
    // vite 相关配置
    server: {
      port: 8080,
      host: '0.0.0.0',
      proxy: {
        '/api': {
          target: VITE_API_HOST,
          changeOrigin:true,
          rewrite: (p) => p.replace(/ ^ /, '')
        },
        '/tenantapi': {
          target: VITE_API_HOST,
          changeOrigin: true,
          rewrite: (path) => path.replace(/ ^ /, '')
        }
      },
    },
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove();
                }
              }
            }
          }
        ]
      }
    }
  }
})
